package com.isscollage.controller;

import java.util.Date;
import java.util.List;
import java.util.Map;

import javax.servlet.http.HttpSession;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;

import com.github.pagehelper.PageInfo;
import com.isscollage.entity.OrderHeader;
import com.isscollage.entity.Passenger;
import com.isscollage.entity.TicketOrder;
import com.isscollage.entity.Users;
import com.isscollage.search.OrderSearch;
import com.isscollage.service.OrderService;
import com.isscollage.service.UserService;

@Controller
@RequestMapping("order")
public class OrderController {

	@Autowired
	private OrderService orderService;
	@Autowired
	private HttpSession session;
	@Autowired
	private UserService userService;
	
	@RequestMapping("searchTickets")
	public String searchTickets(Map<String,Object> map,OrderSearch orderSearch){
		if(orderSearch!=null && orderSearch.getStartStation()!="" && 
				orderSearch.getEndStation()!="" && orderSearch.getStartTime()!=null){
			List<TicketOrder> ticketOrderList=orderService.searchTicketOrderByCon(orderSearch);
			if(ticketOrderList!=null && ticketOrderList.size()>0){
				map.put("list", ticketOrderList);
				map.put("orderSearch", orderSearch);				
			}else{
				map.put("list", null);
				map.put("orderSearch", null);			
			}
		}
		return "order/ticketOrder";
	}
	@RequestMapping("yuding")
	public String yuding(Map<String,Object> map,HttpSession session, String ticketOrderId){
		TicketOrder ticketOrder=orderService.getTicketOrderById(ticketOrderId);
		Users user = (Users) session.getAttribute("user");
		if(user!=null){
			List<Passenger> passenger=userService.searchAllPassengers(user.getUserId());
			map.put("passengers", passenger);
		}
		map.put("ticketOrder", ticketOrder);
		return "order/reservation";
	}
	

	@RequestMapping("saveOrder")
	public String saveOrder(Model model,OrderHeader orderHeader){
		orderHeader=orderService.saveOrder(orderHeader);
		model.addAttribute("orderHeader", orderHeader);
		return "order/buyOrder";
	}
	
	@RequestMapping("payOrder")
	@ResponseBody
	public boolean payOrder(String orderId){
		boolean flag=orderService.payOrder(orderId);
		return flag;
	}
	
	@RequestMapping("cancelOrder")
	@ResponseBody
	public boolean cancelOrder(String orderId){
		boolean flag=orderService.cancelOrder(orderId);
		return flag;
	}
	
	@RequestMapping("checkOrder")
	@ResponseBody
	public boolean checkOrder(){
		return orderService.checkOrder();
	}
	
	@RequestMapping("nonComOrder")
	public String nonComOrder(Model model){
		OrderSearch search=new OrderSearch();
		search.setOrderFlag(0);
		search.setType(null);
		List<OrderHeader> list=orderService.searchOrderByCon(search);
		if(list!=null && list.size()>0){
			model.addAttribute("orderHeader", list.get(0));
		}else{
			model.addAttribute("orderHeader", null);
		}	
		return "order/noComOrder";
	}
	
	@RequestMapping("comOrder")
	public String comOrder(Model model,OrderSearch search){
		if(search!=null && search.getOrderFlag()!=null){
			List<OrderHeader> list=orderService.searchOrderByCon(search);
			PageInfo<OrderHeader> page=new PageInfo<>(list,3);
			model.addAttribute("page", page);
			model.addAttribute("search", search);
		}else{
			search.setOrderFlag(1);
			search.setType(1);
			search.setStartDate(new Date());
			search.setEndDate(new Date());
			PageInfo<OrderHeader> page = new PageInfo<>();
			model.addAttribute("page", page);
			model.addAttribute("search", search);			
		}		
		return "order/comOrder";
	}
	
	@RequestMapping("returnTicket")
	@ResponseBody
	public boolean returnTicket(String detailId){		
		return orderService.returnTicket(detailId);
	}
	
	@RequestMapping("gotoTicket")
	@ResponseBody
	public boolean gotoTicket(String detailId){
		return orderService.gotoTicket(detailId);
	}
	
	@RequestMapping("deleteTicket")
	@ResponseBody
	public boolean deleteTicket(String detailId){
		return orderService.deleteTicket(detailId);
	}
}
